package main

import (
	"fmt"
	"regexp"
	"testgin0012/userManager/model"
	"testgin0012/userManager/service"
)

const (
	RepPhone = "(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}"
	RepEmail = "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+"
)

type CustomerView struct {
	service  *service.CusTomerService
	exisFlag bool
	choice   byte
}

func (c *CustomerView) showList() {
	if len(c.service.List()) == 0 {
		fmt.Println("目前没有用户")
		return
	}
	fmt.Println(service.GetTableHeader())
	for _, customer := range c.service.List() {
		fmt.Println(customer.ToString())
	}
}

func (c *CustomerView) deleteCustomer() {
	var id int
	fmt.Print("请输入用户的编号:")
	for {
		fmt.Scanln(&id)
		_, err := c.service.Find(id)
		if !err {
			fmt.Print("用户未找到! 请重新输入:")
			continue
		}
		break
	}
	c.service.Delete(id)
}

func (c *CustomerView) exit() {
	var sure string
	fmt.Print("确认要退出,y/n?:")
	fmt.Scanln(&sure)
	for {
		if sure == "Y" || sure == "y" {
			c.exisFlag = true
			break
		} else if sure == "n" || sure == "N" {
			break
		} else {
			fmt.Print("输入有误! 请重新输入:")
			fmt.Scanln(&sure)
		}
	}
}

func (c *CustomerView) updateCustomer() {
	var id int
	fmt.Print("请输入用户的编号:")
	fmt.Scanln(&id)

	cus, err := c.service.Find(id)
	if !err {
		fmt.Println("用户未找到")
		return
	}

	var name string
	fmt.Print("请输入用户名:")
	fmt.Scanln(&name)
	if name != "" {
		cus.Name = name
	}

	var gender string
	fmt.Print("请输入用户性别:")
	for {
		fmt.Scanln(&gender)
		if gender != "" {
			if gender != "女" && gender != "男" {
				fmt.Print("输入有误! 请重新输入:")
				continue
			} else {
				cus.Gender = gender
			}
		}
		break
	}

	var age uint8
	fmt.Print("请输入用户年龄:")
	fmt.Scanln(&age)
	if age > 0 {
		cus.Age = age
	}

	var phone string
	fmt.Print("请输入用户手机号:")
	for {
		fmt.Scanln(&phone)
		if phone != "" {
			if matched, ok := regexp.MatchString(RepPhone, phone); ok != nil ||
				!matched {
				fmt.Print("输入手机号格式有误! 请重新输入:")
				continue
			} else {
				cus.Phone = phone
			}
		}
		break
	}

	var email string
	fmt.Print("请输入用户email:")
	for {
		fmt.Scanln(&email)
		if email != "" {
			if matched, ok := regexp.MatchString(RepEmail, email); !matched ||
				ok != nil {
				fmt.Print("输入email格式有误! 请重新输入:")
				continue
			} else {
				cus.Email = email
			}
		}
		break
	}
}
func (c *CustomerView) addCustomer() {
	var name string
	fmt.Print("请输入用户名:")
	for {
		fmt.Scanln(&name)
		if name == "" {
			fmt.Println("不能为空!")
			fmt.Print("请重新输入:")
		} else {
			break
		}
	}

	var gender string
	fmt.Print("请输入用户性别:")
	for {
		fmt.Scanln(&gender)
		if gender == "" {
			fmt.Print("不能为空!")
		} else if gender != "女" && gender != "男" {
			fmt.Print("输入有误!")
		} else {
			break
		}
		fmt.Print("请重新输入:")
	}

	var age uint8
	fmt.Print("请输入用户年龄:")
	for {
		fmt.Scanln(&age)
		if age == 0 {
			fmt.Print("不能为空!")
		} else {
			break
		}
		fmt.Print("请重新输入:")
	}

	var phone string
	fmt.Print("请输入用户手机号:")
	for {
		fmt.Scanln(&phone)
		if phone == "" {
			fmt.Print("不能为空!")
		} else if matched, ok := regexp.MatchString(RepPhone, phone); ok != nil ||
			!matched {
			fmt.Print("输入手机号格式有误")
		} else {
			break
		}
		fmt.Print("请重新输入:")
	}

	var email string
	fmt.Print("请输入用户email:")
	for {
		fmt.Scanln(&email)
		if email == "" {
			fmt.Print("不能为空!")
		} else if matched, ok := regexp.MatchString(RepEmail, email); !matched ||
			ok != nil {
			fmt.Print("输入email格式有误")
		} else {
			break
		}
		fmt.Print("请重新输入:")
	}

	c.service.Add(&model.Customer{
		Name:   name,
		Gender: gender,
		Age:    age,
		Phone:  phone,
		Email:  email,
	})
}
func (c *CustomerView) showMenu() {
	for {
		fmt.Println("---------------客户信息管理软件--------------")
		fmt.Println("1.添加客户")
		fmt.Println("2.修改客户")
		fmt.Println("3.删除客户")
		fmt.Println("4.客户列表")
		fmt.Println("5.退   出")
		fmt.Print("请选择(1-5):")
		fmt.Scanln(&c.choice)

		switch c.choice {
		case 4:
			c.showList()
		case 3:
			c.deleteCustomer()
		case 5:
			c.exit()
		default:
			fmt.Println("输入有误!")
		case 2:
			c.updateCustomer()
		case 1:
			c.addCustomer()
		}
		if c.exisFlag {
			break
		}
	}
}
func New() *CustomerView {
	return &CustomerView{
		service: service.MakeCusTomerService(),
	}
}
func main() {
	c := New()
	c.showMenu()
}
